//
//  VIPZegoVideoCaptureFromScreen.m
//  TTProduct
//
//  Created by Taurin on 2020/1/2.
//  Copyright © 2020 TTProduct. All rights reserved.
//

#import "VIPZegoVideoCaptureFromScreen.h"
#import "VIPZegoConfig.h"

@interface VIPZegoVideoCaptureFromScreen ()
@property (nonatomic, strong) id<ZegoVideoCaptureClientDelegate> client;
@property (nonatomic, assign) BOOL isCapturing;

@end

@implementation VIPZegoVideoCaptureFromScreen

- (void)zego_allocateAndStart:(id<ZegoVideoCaptureClientDelegate>)client {
    self.client = client;
    
    [self.client setFillMode:kZegoVideoFillMode];
}

- (void)zego_stopAndDeAllocate {
    [self.client destroy];
    self.client = nil;
}

- (int)zego_startCapture {
    self.isCapturing = YES;
    return 0;
}

- (int)zego_stopCapture {
    self.isCapturing = NO;
    return 0;
}

// 接收视频帧数据
- (void)captureOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnectrion:(AVCaptureConnection*)connection {
    
    CMTime pts = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
    CVImageBufferRef buffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    
    if (self.isCapturing) {
        [self.client onIncomingCapturedData:buffer withPresentationTimeStamp:pts];
    }
}

//  接收视频帧数据
- (void)captureOutputPixelBuffer:(CVPixelBufferRef)pixelBuffer timeStamp:(CMTime)time {
    if (self.isCapturing) {
        [self.client onIncomingCapturedData:pixelBuffer withPresentationTimeStamp:time];
    }
}

@end


@interface VIPZegoVideoCaptureScreenFactory ()

@end

@implementation VIPZegoVideoCaptureScreenFactory

- (id<ZegoVideoCaptureDevice>)zego_create:(NSString *)deviceId {
    if (self.screenDevice == nil) {
        _screenDevice = [[VIPZegoVideoCaptureFromScreen alloc] init];
    }
    
    return self.screenDevice;
}

- (void)zego_destroy:(id<ZegoVideoCaptureDevice>)device {
    _screenDevice = nil;
}

@end
